﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using TrainingCenterSystem.Utils;

namespace TrainingCenterSystem.UserManagement
{
    public class UserFileWriter
    {
        #region private members
        private XmlWriterSettings _settings;
        private XmlWriter _xmlWriter;
        private ArrayList _users;
        private List<Supervisor> _approvalList;
        #endregion

        #region setters and getters
        #endregion

        #region constructor(s)
        public UserFileWriter(string filename, ArrayList users, List<Supervisor> approval)
        {
            _users = users;
            _approvalList = approval;
            this.init(filename);
            if (_approvalList==null)
                this.writeUserFile(true);
            else
                this.writeUserFile(false);
        }
        #endregion

        #region methods
        private void init(string filename)
        {
            _settings = new XmlWriterSettings();
            _settings.NewLineChars = Environment.NewLine;
            _settings.NewLineOnAttributes = true;
            _settings.NewLineHandling = NewLineHandling.Replace;
            _settings.NewLineOnAttributes = false;
            _settings.CloseOutput = true;
            _settings.Indent = true;
            _xmlWriter = XmlWriter.Create(filename, _settings);
        }

        private void writeUserFile(bool initial)
        {
            _xmlWriter.WriteStartDocument();

            // Write comments
            _xmlWriter.WriteComment("File for storing system users");

            // Root node
            _xmlWriter.WriteStartElement("UserData");
            // User node
            _xmlWriter.WriteStartElement("SharpVTCUsers");

            for (int i = 0; i < _users.Count; i++)
            {
                User user = (User)_users[i];
                if (user is Supervisor)
                {
                    _xmlWriter.WriteStartElement("Supervisor");
                    this.writeUserData(user);
                    _xmlWriter.WriteEndElement();
                }
                else if (user is Student)
                {
                    _xmlWriter.WriteStartElement("Student");
                    this.writeUserData(user);
                    _xmlWriter.WriteEndElement();
                }
            }

            _xmlWriter.WriteEndElement();

            if (!initial)
            {
                _xmlWriter.WriteStartElement("Approval");

                for (int i = 0; i<_approvalList.Count;i++)
                {
                    Supervisor sup = _approvalList[i];
                    _xmlWriter.WriteStartElement("Supervisor");
                    this.writeUserData(sup);
                    _xmlWriter.WriteEndElement();
                }

                _xmlWriter.WriteEndElement();
            }

            _xmlWriter.WriteEndElement();
            // write the end of document
            _xmlWriter.WriteEndDocument();            

            // close writer
            _xmlWriter.Close();                       
        }

        private void writeUserData(User aUser)
        {
            _xmlWriter.WriteStartElement("UserID");
            _xmlWriter.WriteString(aUser.UserID);
            _xmlWriter.WriteEndElement();

            _xmlWriter.WriteStartElement("Username");
            _xmlWriter.WriteString(aUser.Username);
            _xmlWriter.WriteEndElement();

            _xmlWriter.WriteStartElement("Password");
            _xmlWriter.WriteString(StringEncryption.EncryptString(aUser.Password,"sharpvtcpwd"));
            _xmlWriter.WriteEndElement();

            _xmlWriter.WriteStartElement("EMail");
            if (aUser.EMail!=null)
                _xmlWriter.WriteString(aUser.EMail);
            else
                _xmlWriter.WriteString("");
            _xmlWriter.WriteEndElement();

            _xmlWriter.WriteStartElement("Active");
            _xmlWriter.WriteString(aUser.IsActive.ToString());
            _xmlWriter.WriteEndElement();
        }        
        #endregion
    }
}
